﻿using FineUI;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.PersonalCenter.BLL;

namespace ZHDJ.PersonalCenter.Module.VolunteerService
{
    public partial class VolunteerQuotationEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyVolunteerQuotation quotation = DB.PartyVolunteerQuotations.Find(id);
                if (quotation == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = quotation.Content;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                PartyVolunteerQuotation quotation = DB.PartyVolunteerQuotations.Find(id);
                if (quotation == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                quotation.Content = txtTitle.Text.Trim();
                quotation.ModifyTime = DateTime.Now;
                quotation.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyVolunteerQuotation quotation = new PartyVolunteerQuotation();
                quotation.ID = StringHelper.NewID;
                quotation.Content = txtTitle.Text.Trim();
                quotation.CreateTime = DateTime.Now;
                quotation.ModifyTime = DateTime.Now;
                quotation.CreateBy = CurrentUser.LoginID;
                quotation.ModifyBy = CurrentUser.LoginID;

                DB.PartyVolunteerQuotations.Add(quotation);

                DB.SaveChanges();
            }          
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}